package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortPoint {
    public static void main(String[] args) {
        List<Point> list = new ArrayList<>();
        list.add(new Point(4,7));
        list.add(new Point(22,43));
        list.add(new Point(13,42));
        list.add(new Point(43,32));
        list.add(new Point(2,31));
        list.add(new Point(32,5));
        list.add(new Point(1,4));
        System.out.println("list原始数据"+list);
        Collections.sort(list,(Point o1, Point o2) ->{
                int len1 = o1.getX()*o1.getX()+o1.getX()*o1.getX();
                int len2 = o2.getX()*o2.getX()+o2.getX()*o2.getX();
                return len1-len2; //升序
                //return len2-len1; //降序
                //return o1.getX()-o2.getX(); //按x坐标升序
                //return o2.getY()=o1.getY(); //按y坐标降序
        });


        //自定义排序规则
        Collections.sort(list, new Comparator<Point>() {
            public int compare(Point o1, Point o2) {
                int len1 = o1.getX()*o1.getX()+o1.getX()*o1.getX();
                int len2 = o2.getX()*o2.getX()+o2.getX()*o2.getX();
                return len1-len2; //升序
                //return len2-len1; //降序
                //return o1.getX()-o2.getX(); //按x坐标升序
                //return o2.getY()=o1.getY(); //按y坐标降序
            }
        });
        System.out.println("排序后list"+list);
        list.sort((o1,o2) ->o1.getX()-o2.getX());//按照x升序排列
        System.out.println("排序后list"+list);
    }
}
